<template>
  <van-form @submit="onSubmit">
  <van-cell-group inset>
    <van-field
      v-model=editUser.value
      :name=editUser.key
      :label=editUser.flag
      :placeholder=editUser.flag
      :rules="[{ required: true, message: '请填写'}]"
    />
    </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
</template>





<script setup lang="ts">
import {useRoute,useRouter} from 'vue-router'
import {ref,onMounted} from 'vue'
import myAxios from '../../plugins/myAxios';

const route = useRoute()
const router = useRouter()
const editUser = ref({
  key:route.query.editKey,
  value:route.query.editValue,
  flag:route.query.editFlag
})
const user = ref()


onMounted(()=>{
  getUser()
})

const getUser = async()=>{
  const res = await myAxios.get('/user/current')
  user.value = res.data.data
}

const onSubmit = () => {
  //todo 发送editvalue
  myAxios.post("/user/update",{
    'id':user.value.id,
    [editUser.value.key as string] :editUser.value.value
  }).then(()=>{
    router.replace('/user')
  }).catch((error)=>{
    console.error(error);
  })
};
</script>



<style scoped>

</style>